package webx.actions;

import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IViewReference;
import org.eclipse.ui.PlatformUI;

public class GetSelectedFilesRunnable extends Test implements Runnable {
	
	@SuppressWarnings("rawtypes")
	Map result = null;
	
	public GetSelectedFilesRunnable() {
	}
	
	@SuppressWarnings("rawtypes")
	public GetSelectedFilesRunnable(Map result ) {
		this.result = result;
	}
	public static void main(String[]args){
		System.out.println(new GetSelectedFilesRunnable() instanceof Test );
	}
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void run() {
		try
		{   //org.eclipse.jdt.ui.
			//org.eclipse.jdt.internal.core.PackageFragment
			//org.eclipse.jdt.internal.core.CompilationUnit
			//org.eclipse.jdt.internal.ui.packageview.PackageExplorerPart
			//IViewPart viewPart = null;
			//IViewPart[] viewParts = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getViews();
			//for(int i=0;i<viewParts.length;i++){
				// System.out.print(viewParts[i].getTitle()+"  ;");System.out.println(viewParts[i].getViewSite());
			//}
			
			//IViewPart viewPart0 = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView("org.eclipse.ui.navigator.ProjectExplorer");
			//IViewPart viewPart1 = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView("org.eclipse.jdt.ui.PackageExplorer");
			//IViewPart viewPart2 = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView("org.eclipse.php.ui.explorer");
			String str = "";
			IViewReference[] ref = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getViewReferences();
			//IWorkbenchPage tt = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
			//System.out.println("PlatformUI:"+tt.getPartState(tt.getActivePartReference()));
			for(int i=0;i<ref.length;i++){
				IViewPart viewPart0 = ref[i].getView(false);
				// JOptionPane.showMessageDialog(null, ref[i].getId());
				if(ref[i].getId().toLowerCase().endsWith("explorer")||ref[i].getId().toLowerCase().endsWith("resourcenavigator")){
					//viewPart0.
					//JOptionPane.showMessageDialog(null, ref[i].getId());
					if(viewPart0!=null){
						//System.out.println(viewPart0.getSite()+"||"+viewPart0.getSite().getPage().isPartVisible(viewPart0));
						boolean viewVisible = viewPart0.getSite().getPage().isPartVisible(viewPart0);
						if(!viewVisible){
							continue;
						}
				        StructuredSelection selection = (StructuredSelection) viewPart0.getViewSite().getSelectionProvider().getSelection();
						Iterator itr  = selection.iterator();
			            while(itr.hasNext()){
			            	Object obj = itr.next();
			            	//System.out.println("obj"+(obj.getClass()));
			            	//JOptionPane.showMessageDialog(null, "obj"+(obj.getClass()));
			            	if(obj instanceof IResource){ // IResource
			        			IPath path = ((IResource)obj).getFullPath();
			        			str+=","+path;
			        		}else{ // third
				            	Method getPath = obj.getClass().getMethod("getPath");
				            	if(getPath!=null){
				            		str+=","+getPath.invoke(obj);
				            	}
			            	}
			            	//JOptionPane.showMessageDialog(null, obj.getClass());
			            	//if(obj instanceof org.eclipse.jdt.internal.core.PackageFragmentRoot){
			            		//str+=","+((org.eclipse.jdt.internal.core.PackageFragmentRoot)obj).getPath();
			            	//}
			            	//if(obj instanceof org.eclipse.jdt.internal.core.PackageFragment){
			            		//str+=","+((org.eclipse.jdt.internal.core.PackageFragment)obj).getPath();
			            	//}
			            	//if(obj instanceof org.eclipse.jdt.internal.core.CompilationUnit){
			            		//str+=","+((org.eclipse.jdt.internal.core.CompilationUnit)obj).getPath();
			            	//}
			            	//if(obj instanceof IResource){
			            		//str+=","+((IResource)obj).getFullPath();
			            	//}
			            }
			            
					}
					
				}
			}
			
			result.put("result", str);
		}catch (Throwable e){  
			e.printStackTrace();
		}
	}
	
}
